<?php

class Core_Model_DbTable_Pages extends Zend_Db_Table_Abstract {

    protected $_name = 'pages';
    
    public function getInstruction($id_social_page) {
        $query = $this->select()
                      ->from($this->_name, "description")
                      ->where("id_social_page = '$id_social_page'");
        
        $result = $this->fetchRow($query);
        return $result->description;
    }

    public function getPageInfo($id_social_page, $cols = NULL) {
        $query = $this->select();
        if (!empty($cols)) {
            $query->from($this->_name, $cols);
        } else {
            $query->from($this->_name);
        }

        $query->where("id_social_page = '$id_social_page'");
        return $this->fetchRow($query);
    }

    public function getPoint($id_social_page, $action_type) {
        $cols = array(
            'post_point',
            'comment_point',
            'like_point',
            'vote_point'
        );
        $points = $this->getPageInfo($id_social_page, $cols);
        $result = $points->toArray();
        $point_col = $action_type . '_point';
        return $result[$point_col];
    }
    
    public function insertDefault($id_social_page) {
        $default_data = array (
            'description'       => $this->_getDefaultDescription(),
            'post_point'        => 1,
            'comment_point'     => 1,
            'like_point'        => 1,
            'invite_point'      => 1,
            'share_point'       => 1,
            'background_color'  => 'f4f6fa',
            'update_frequency'  => 24,
            'id_social_page'    => $id_social_page
        );
        
        $this->insert($default_data);
    }
    
    private function _getDefaultDescription() {
        return '<h1><strong>Welcome to Test Topfans TopFans</strong></h1>
                <p>This is the place where your contribution will be recorded and where we recognize who is our most active fan, sometimes we will have gift for persons who is on top of active fans.</p>
                <p>Here is how we calculate your score:</p>
                <p>- Post: 1 point</p>
                <p>- Comment on post: 1 point</p>
                <p>- Like on post: 1 point</p>
                <p>- Invite your friends to like out page: 1 point</p>
                <p>- Share your friends to like our page: 1 point</p>
                <h3><strong>Be Test Topfans top fan and have a chance to win our special gifts.</strong></h3>';
    }
        
    public function getPagesForUpdating($update_frequency, $count, $offset) {
        $query = $this->select()
                      ->from($this->_name, "id_social_page")
                      ->where("MOD($update_frequency, update_frequency) = 0")
                      ->order("update_frequency")
                      ->limit($count, $offset);
        return $this->fetchAll($query);
    }
}
?>
